package com.huawei.hwid.core.d.b;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build.VERSION;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.io.File;
import java.io.IOException;

public abstract class b {
    public static final String a = b.class.getSimpleName();
    private static String b = "";

    static class a {
        static String a(Context context) {
            String str = d(context) + "/Log/";
            File file = new File(str);
            if (file.exists() || file.mkdirs()) {
                return str;
            }
            return null;
        }

        private static String d(Context context) {
            return context.getFilesDir().getAbsolutePath();
        }

        private static boolean a() {
            if ("mounted".equals(Environment.getExternalStorageState()) || !b()) {
                return true;
            }
            return false;
        }

        static String b(Context context) {
            String str = e(context) + "/Log/";
            File file = new File(str);
            if (file.exists() || file.mkdirs()) {
                return str;
            }
            return null;
        }

        private static String e(Context context) {
            String str = null;
            if (a()) {
                File externalFilesDir = context.getExternalFilesDir(str);
                if (externalFilesDir != null) {
                    try {
                        str = externalFilesDir.getCanonicalPath();
                    } catch (IOException e) {
                        e.b(b.a, "Exception", true);
                    }
                }
            }
            return str;
        }

        public static void c(Context context) {
            Object b = b(context);
            if (!TextUtils.isEmpty(b)) {
                e.b(b.a, "deleteOldLogFiles File", true);
                File file = new File(b);
                if (file.exists() && file.isDirectory()) {
                    a(file);
                }
            }
        }

        private static void a(File file) {
            if (file == null) {
                e.b(b.a, "deleteDirInnerPicFileDir == null", true);
                return;
            }
            File[] listFiles = file.listFiles();
            if (listFiles != null) {
                for (File file2 : listFiles) {
                    if ((file2.getName().contains("apphwid.txt") || file2.getName().contains("apphwid.txt.bak") || file2.getName().contains("hwid_advanced_log.txt")) && !file2.delete()) {
                        Log.i("deleteDirInnerPicFile", "delete file fail");
                    }
                }
            }
        }

        @TargetApi(9)
        private static boolean b() {
            if (VERSION.SDK_INT >= 9) {
                return Environment.isExternalStorageRemovable();
            }
            return true;
        }
    }

    abstract void a(String str, String str2);

    abstract void b(String str, String str2);

    abstract void c(String str, String str2);

    public static String a() {
        return b;
    }

    static String a(Context context) {
        String str = "";
        if (context != null && "com.huawei.hwid".equals(context.getPackageName())) {
            try {
                str = "HwID_APK_log[" + context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName + "]:";
            } catch (NameNotFoundException e) {
                Log.e("hwid", "getVersionTag error" + e.getClass().getSimpleName());
            }
        }
        if (TextUtils.isEmpty(str)) {
            return HwAccountConstants.HWID_SDK_LOG;
        }
        return str;
    }
}
